package com.cloudsigns.factorymanager2.project.utils;

import android.text.TextUtils;
import android.view.View;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by tony on 2018/2/9.
 */

public class DateUtil {
    private static SimpleDateFormat df_yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat df_MM_dd = new SimpleDateFormat("MM/dd");
//    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss

    public static boolean compareDate(CharSequence yearText, CharSequence monthText, CharSequence dayText, View view) {

        try {
            Date selected = df_yyyy_MM_dd.parse(yearText + "-" + monthText + "-" + dayText);
            Date today = new Date();
            if (selected.getTime() - today.getTime() < 0) {
                view.setEnabled(true);
                view.setClickable(true);
                return true;
            }
        } catch (ParseException e) {

            e.printStackTrace();
        }
        view.setEnabled(false);
        view.setClickable(false);
        return false;
    }

    public static String getYyyy_Mm_Dd(long millis) {
        if (millis == 0)
            return "";

        return df_yyyy_MM_dd.format(millis);
    }

    public static String getMm_Dd(long millis) {
        if (millis == 0)
            return "";

        return df_MM_dd.format(millis);
    }

    public static long getMillis(String yyyy_MM_dd_str) {
        try {
            if (!TextUtils.isEmpty(yyyy_MM_dd_str)) {
                Date date = df_yyyy_MM_dd.parse(yyyy_MM_dd_str);
                return date.getTime();
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }


    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }

    public static int getBetweenDays(long beginDate, long currentDate) {
        int days = -1;
        if (beginDate > 0 && currentDate > 0 && beginDate <= currentDate) {
            long one_day_mill = 24 * 60 * 60 * 1000;

            days = (int) ((currentDate - beginDate) / (one_day_mill));
        }
        return days;
    }
    public static long getZeroMills(long current ) {
        long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
        return zero;
    }

    /**
     * <pre>
     * 根据指定的日期字符串获取星期几
     * </pre>
     *
     * @param strDate 指定的日期字符串(yyyy-MM-dd 或 yyyy/MM/dd)
     * @return week
     * 星期几(MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY)
     */
    public static String getWeekByDateStr(String strDate) {
        int year = Integer.parseInt(strDate.substring(0, 4));
        int month = Integer.parseInt(strDate.substring(5, 7));
        int day = Integer.parseInt(strDate.substring(8, 10));

        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);

        String week = "";
        int weekIndex = c.get(Calendar.DAY_OF_WEEK);

        switch (weekIndex) {
            case 1:
                week = "周日";
//                week = "SUNDAY";
                break;
            case 2:
                week = "周一";
//                week = "MONDAY";
                break;
            case 3:
                week = "周二";

//                week = "TUESDAY";
                break;
            case 4:
                week = "周三";

//                week = "WEDNESDAY";
                break;
            case 5:
                week = "周四";

//                week = "THURSDAY";
                break;
            case 6:
                week = "周五";

//                week = "FRIDAY";
                break;
            case 7:
                week = "周六";

//                week = "SATURDAY";
                break;
        }
        return week;
    }

}
